#include <tinyhal.h>
#include "..\..\..\DeviceCode\Targets\Native\FM3\DeviceCode\FM3.h"

#ifdef __GNUC__
void __attribute__((section("SectionForBootstrapOperations"))) BootstrapCode_GPIO()
#else
void __section(SectionForBootstrapOperations) BootstrapCode_GPIO()
#endif
{
    // MB9BF506N
    // P00      P10      (AN00)  P30 D6        P40       P50 D0   P60
    // P01      P11      (AN01)  P31 D7        P41       P51 D1   P61
    // P02      P12 A9   (AN02)  P32 MDQM0 LB  P42 A20   P52 D2   P62 A0
    // P03      P13 A8   (AN03)  P33 MDQM1 UB  P43 A19   P53 D3   P63 A1
    // P04      P14      (AN04)  P34           P44 A18   P54 D4
    // P05      P15      (AN05)  P35           P45 A17   P55 D5
    // P06      P16 MOEX (AN06)  P36 MCSX3     P46       P56
    // P07      P17 MWEX (AN07)  P37 MCSX2     P47       P57
    // P08      P18 D8   (AN08)  P38           P48 A16   P58
    // P09      P19 D9   (AN09)  P39           P49 A15   P59
    // P0A A7   P1A D10  (AN10)  P3A           P4A A14   P5A
    // P0B A6   P1B D11  (AN11)  P3B           P4B A13   P5B
    // P0C A5   P1C D12  (AN12)  P3C           P4C A12   P5C
    // P0D A4   P1D D13  (AN13)  P3D           P4D A11   P5D
    // P0E A3   P1E D14  (AN14)  P3E           P4E A10   P5E
    // P0F A2   P1F D15  (AN15)  P3F           P4F       P5F
#ifdef MB9BF506N
    FM3_GPIO->ADE       = 0x00000033;   //AN00,AN01,,,AN04,AN05
    FM3_GPIO->PFR0      = 0xfc1f;       //P0A...P0F/A7...A2
    FM3_GPIO->PFR1      = 0xffcc;       //P12...P1F/A9,A8,,,OE,WE,D8..D15
    FM3_GPIO->PFR3      = 0x00cf;       //D6,D7,LB,UB,,,CS3,CS2
    FM3_GPIO->PFR4      = 0x7f3c;       //A20,A19,A18,A17,,,A16..A10
    FM3_GPIO->PFR5      = 0x003f;       //D0..D5
    FM3_GPIO->PFR6      = 0x0008;       //A1
    FM3_GPIO->EPFR10    = 0x0fff833b;
    // CS3 - SRAM 2MB
    FM3_EXBUS->MODE3    = (1 << 0)      //WDTH 16bit
                        | (1 << 2);     //RBMON
    FM3_EXBUS->TIM3     = (0 << 28)     //WIDLC
                        | (2 << 24)     //WWEC
                        | (0 << 20)     //WADC
                        | (2 << 16)     //WACC
                        | (1 << 12)     //RIDLC
                        | (0 << 8)      //FRADC
                        | (0 << 4)      //RADC
                        | (6 << 0);     //RACC
    FM3_EXBUS->AREA3    = (0x0f << 16)  //MASK 2MB
                        | (0x30 << 0);  //ADDR 0x63000000
    // CS2 - LCD
    FM3_EXBUS->MODE2    = (0 << 0)      //WDTH 8bit
                        | (1 << 2);     //RBMON
    FM3_EXBUS->TIM2     = (0 << 28)     //WIDLC
                        | (2 << 24)     //WWEC
                        | (0 << 20)     //WADC
                        | (2 << 16)     //WACC
                        | (1 << 12)     //RIDLC
                        | (0 << 8)      //FRADC
                        | (0 << 4)      //RADC
                        | (6 << 0);     //RACC
    FM3_EXBUS->AREA2    = (0x0f << 16)  //MASK 2MB
                        | (0x20 << 0);  //ADDR 0x62000000
#endif
}
